﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Order
{
    /// <summary>
    /// Interaction logic for OrderEditWindow.xaml
    /// </summary>
    public partial class OrderEditWindow : Window
    {
        private System.Collections.ObjectModel.ObservableCollection<OrderGoods> ogs = new System.Collections.ObjectModel.ObservableCollection<OrderGoods>();

        public Mengsk.ProxyDelivery.Domain.Order Order { get; set; }

        public OrderEditWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.btnAdd_Click(this.btnAdd, new RoutedEventArgs());
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.dgvOrderGoods.ItemsSource = this.ogs;
                this.ogs.Add(new OrderGoods { Count = 1 });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvOrderGoods.SelectedCells.Count < 1)
            {
                return;
            }

            OrderGoods og = this.dgvOrderGoods.SelectedCells[0].Item as OrderGoods;
            if (og == null)
            {
                MessageBox.Show("未选择数据");
                return;
            }

            this.ogs.Remove(og);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var os = ServiceContainer.GetService<OrderService>();
                DateTime minTime = os.GetDBMinTime();

                //检测商品信息
                foreach (var og in this.ogs)
                {
                    if (string.IsNullOrWhiteSpace(og.Vendor) ||
                        string.IsNullOrWhiteSpace(og.Number) ||
                        string.IsNullOrWhiteSpace(og.Color) ||
                        string.IsNullOrWhiteSpace(og.Size) ||
                        og.Count < 0)
                    {
                        throw new Exception("商品信息不能为空");
                    }

                    if (string.IsNullOrWhiteSpace(og.Edtion))
                    {
                        og.Edtion = "";
                    }
                    os.AddOrderGoods(this.Order.Id, og.Vendor, og.Number, og.Edtion, og.Color, og.Size, og.Count);
                }
                MessageBox.Show("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
